package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.sk.waste.app.R
import com.sk.waste.app.application.MyApplication
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.models.TenderModel
import com.sk.waste.app.models.UserModel
import com.sk.waste.app.utils.BaseUtil

/**
 *  @time 2025/01/07  15:37
 *  @desc 招标信息
 */
class TenderAdapter(private val context: Activity, val type: Int, val list: List<TenderModel>, val callback: LCInterface.LcCallBack) :
    RecyclerView.Adapter<TenderAdapter.ViewHolder>() {
    var userInfo: UserModel? = null

    init {
        userInfo = MyApplication.instance.getUserInfo()
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TenderAdapter.ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_tender, parent, false))
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {

        val item = list[position]
        holder.ttvName.text = item.title
        holder.ttvContent.text = item.contentText
        if (userInfo?.isVip() == true) {
            holder.ttvPerson.text = item.person
            holder.ttvPhone.text = item.phone
        } else {
            holder.ttvPerson.text = BaseUtil.getNameHideStr(item.person)
            holder.ttvPhone.text = BaseUtil.getPhoneHideStr(item.phone)
        }

        if (type == 0)//0 列表，1 收藏
        {
            holder.ttvTime.text = item.startDate
            holder.bttDelete.visibility = View.GONE
        } else {
            holder.ttvTime.text = item.favoriteDate
            holder.bttDelete.visibility = View.VISIBLE
        }
        if ("有效" == item.status) {
            holder.ttvName.setTextColor(context.getColor(R.color.color_text_deep))
            holder.ttvTime.setTextColor(context.getColor(R.color.color_text_deep))
            holder.imgState.visibility = View.GONE
        } else {
            holder.ttvName.setTextColor(context.getColor(R.color.color_text_gray))
            holder.ttvTime.setTextColor(context.getColor(R.color.color_text_gray))
            holder.imgState.visibility = View.VISIBLE
        }
        if (!LCBaseUtil.isNull(item.image)) {
            Glide.with(context).load(NetWorkConfig.NET_URL_IMA_ROOT + item.image).transform(CenterCrop(), RoundedCorners(20)).error(R.mipmap.img_item_default)
                .into(holder.imgView);
        }

        holder.ttvPhone.tag = position
        holder.ttvPhone.setOnClickListener {
            if (userInfo?.isVip() == true) {
                val cp = holder.ttvPhone.tag as Int
                BaseUtil.callPhone(context, list[cp].phone)
            }
        }

        holder.bttDelete.tag = position
        holder.bttDelete.setOnClickListener {
            val currentSelect = holder.rootView.tag as Int
            callback.callBack(holder.bttDelete.id, currentSelect)
        }

        holder.rootView.tag = position
        holder.rootView.setOnClickListener {
            val currentSelect = holder.rootView.tag as Int
            callback.callBack(holder.rootView.id, currentSelect)
        }

    }

    fun mNotifyDataSetChanged() {
        userInfo = MyApplication.instance.getUserInfo()
        notifyDataSetChanged()
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ttvName: TextView = view.findViewById(R.id.item_tender_name)
        val ttvTime: TextView = view.findViewById(R.id.item_tender_time)
        val imgView: ImageView = view.findViewById(R.id.item_tender_image)
        val ttvContent: TextView = view.findViewById(R.id.item_tender_content)
        val ttvPerson: TextView = view.findViewById(R.id.item_tender_person)
        val ttvPhone: TextView = view.findViewById(R.id.item_tender_phone)
        val bttDelete: ImageButton = view.findViewById(R.id.item_tender_delete)
        val imgState: ImageView = view.findViewById(R.id.item_tender_state)

        val rootView: FrameLayout = view.findViewById(R.id.item_root_layout)
    }


}